//
//  ScrollViewController.m
//  滚动视图
//
//  Created by yanxin on 15/5/28.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//
#import "RootViewController.h"
#import "ScrollViewController.h"
#import "MMDrawerController.h"
#import "DuanziViewController.h"
#import "CustomizedNavigationController.h"
#import "MMExampleDrawerVisualStateManager.h"
#import "Define.h"

@interface ScrollViewController ()<UIScrollViewDelegate>

@end

@implementation ScrollViewController
- (void)dealloc
{
    [_scrollView release];
    [_pageControl release];
    [_imageView release];
    [super dealloc];
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]autorelease];
        _scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds)*4,CGRectGetHeight(self.view.bounds));
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;//横向滑动的滚轴不显示
        _scrollView.showsVerticalScrollIndicator = NO;//纵向滑动的滚轴不显示
        _scrollView.directionalLockEnabled = YES;//任意方向滑动是否锁定   YES
    }return _scrollView;
}
- (UIPageControl *)pageControl
{
    if (!_pageControl)
    {
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(70, self.view.bounds.size.height - 43, self.view.bounds.size.width-140, 40)];
        _pageControl.numberOfPages = 4;//指定小圆点的个数
        _pageControl.currentPage = 0;
//        _pageControl.backgroundColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor clearColor];
    }return _pageControl;
}
- (UIImageView *)imageView
{
    if (!_imageView)
    {
        self.imageView = [[UIImageView alloc]init];
    }return _imageView;
}
//============================================================================================
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.scrollView.delegate = self;
    self.scrollView.tag = 110;
    [self.view addSubview:self.scrollView];
//--------------------------------------------------------------------------
    for (int i = 0; i < 4; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%d.png",i+1];
       
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        imageView.image = [UIImage imageNamed:imageName];
        [self.scrollView addSubview:imageView];
        if (i == 3)
        {
            imageView.userInteractionEnabled = YES;
            UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]autorelease];
            button.frame = CGRectMake(0 , 0, kSCREENWIDTH, kSCREENHEIGHT);
//            button.backgroundColor = [UIColor cyanColor];
            [imageView addSubview:button];
            [button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchDown];
        }
    }
//------------------------------------------------------------------------------------------
    [self.pageControl addTarget:self action:@selector(handlePageControlAction:) forControlEvents:UIControlEventValueChanged];
//    self.pageControl.tag = 222;
    [self.view addSubview:self.pageControl];
    [self.pageControl release];
    
}
//点击 pagecontrol 实现图片切换
- (void)handlePageControlAction:(UIPageControl *)sender
{
    UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:110];
    CGPoint offSet = CGPointMake(sender.currentPage * CGRectGetWidth(scrollview.bounds), 0);
    [scrollview setContentOffset:offSet animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
    //通过最终得到的 contentofset 的值来确定 pagecontrol 当前应该指示第几个视图
//    NSInteger index = scrollView.contentOffset.x / CGRectGetWidth(self.view.bounds) ;
    

//    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:222];
    NSInteger index = scrollView.contentOffset.x / (self.view.bounds.size.width );
    //修改currentpage属性
    self.pageControl.currentPage = index;
}

- (void)handleButtonAction:(UIButton *)sender
{

    RootViewController *leftVC = [[RootViewController alloc] init];
    CustomizedNavigationController *leftNaviC = [[CustomizedNavigationController alloc] initWithRootViewController:leftVC];
    [leftVC release];

    DuanziViewController *duanziVC = [[DuanziViewController alloc] init];
    CustomizedNavigationController *duanziNaviC = [[CustomizedNavigationController alloc] initWithRootViewController:duanziVC];
    [duanziVC release];

    //抽屉管理 第三方
    //该第三方既可以只实现打开左侧栏也可以实现打开右侧栏,还可以同时都实现
    //    MMDrawerController *rooVC = [[MMDrawerController alloc] initWithCenterViewController:navigationVC leftDrawerViewController:leftNavigationVC rightDrawerViewController:rightNavigationVC];

    //只实现打开左侧栏
    MMDrawerController *rooVCLeft = [[MMDrawerController alloc] initWithCenterViewController:duanziNaviC leftDrawerViewController:leftNaviC];
    [duanziNaviC release];
    [leftNaviC release];
    //只实现打开右侧栏
    //MMDrawerController *rooVCRight = [[MMDrawerController alloc] initWithCenterViewController:navigationVC rightDrawerViewController:thirdVC];



    //指定window的根视图


    //测了门的宽度
    [rooVCLeft setMaximumLeftDrawerWidth:170];
    //设置侧拉门开与关的动画
    [rooVCLeft setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [rooVCLeft setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    //侧开内容展示效果
    //设置向左滑动打开右侧栏
    //    [[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:MMDrawerAnimationTypeParallax];
    //设置向右滑动打开左侧栏
    [[MMExampleDrawerVisualStateManager sharedManager] setLeftDrawerAnimationType:MMDrawerAnimationTypeSwingingDoor];

    //
    [rooVCLeft setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
        MMDrawerControllerDrawerVisualStateBlock block;
        block = [[MMExampleDrawerVisualStateManager sharedManager]
                 drawerVisualStateBlockForDrawerSide:drawerSide];
        if(block){
            block(drawerController, drawerSide, percentVisible);
        }
    }];
    

    [self presentViewController:rooVCLeft animated:YES completion:nil];
    [rooVCLeft release];
    
 
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
